package com.xialinrui.utils;

import com.xialinrui.entity.Todo;
import com.xialinrui.vo.Node;

import java.util.ArrayList;
import java.util.List;

import static java.lang.Long.parseLong;

public class TodoUtils {
    public static boolean contain(Long id, List<Node> nodes) {
        for (Node node : nodes) {
            if (node.getAreaId().equals(id)) {
                return true;
            }
        }
        return false;
    }

    public static Node get(Long id, Node cur) {
        if (cur.getAreaId().equals(id)) {
            return cur;
        }
        if (cur.getChildren() == null) {
            return null;

        }
        for (Node child : cur.getChildren()) {
            Node node = get(id, child);
            if (node != null) {
                return node;
            }
        }
        return null;
    }

    public static Node getFirst(Long id, List<Node> resList) {
        for (Node node : resList) {
            if (id.equals(node.getAreaId())) {
                return node;
            }
        }
        return null;
    }

    public static List<Node> build(List<Todo> todos) {
        List<Node> resList = new ArrayList<>();
        for (Todo todo : todos) {
            if (todo.getType() == 1) {
                String[] areaDetailsStr = todo.getAreaDetail().split("_");
                Long[] areaDetails = new Long[areaDetailsStr.length];
                for (int i = 0; i < areaDetailsStr.length; i++) {
                    areaDetails[i] = parseLong(areaDetailsStr[i]);
                }
                String[] nameDetails = todo.getName().split("_");
                //每一条数据的总长度
                Integer N = areaDetails.length;
                Double moneyNumbers = todo.getMoneyNumbers();
                Integer loveNumbers = todo.getLoveNumbers();
                //最后一个节点，要存储任务的详细信息（非文件夹）
                Node nodeLast = new Node(areaDetails[N - 1], nameDetails[N - 1], moneyNumbers, loveNumbers, null, 1);
                //如果只有一个节点，第一个就是最后一个，直接返回
                if (N == 1) {
                    resList.add(nodeLast);
                    continue;
                }
                Node nodeFirst;
                //在res表中查看有没有首节点
                Node firstIfHas = getFirst(areaDetails[0], resList);
                //创建首节点
                if (firstIfHas == null) {
                    nodeFirst = new Node(areaDetails[0], nameDetails[0], null, null, new ArrayList<>(), 0);
                    resList.add(nodeFirst);
                } else {
                    nodeFirst = firstIfHas;
                }
                //遍历每一个节点
                for (int i = 1; i < N; i++) {
                    //当前来到最后一个节点，直接挂载退出
                    if (i == N - 1) {
                        Node parent = get(areaDetails[i - 1], nodeFirst);
                        parent.getChildren().add(nodeLast);
                    }
                    Node parent = get(areaDetails[i - 1], nodeFirst);
                    //如果之前有这个节点了，无需重复添加 跳过
                    if (contain(areaDetails[i], parent.getChildren())) {
                        continue;
                    } else {//否则 创建节点 添加
                        parent.getChildren().add(new Node(areaDetails[i], nameDetails[i], null, null, new ArrayList<>(), 0));
                    }
                }
            } else {
                String[] areaDetailsStr = todo.getAreaDetail().split("_");
                Long[] areaDetails = new Long[areaDetailsStr.length];
                for (int i = 0; i < areaDetailsStr.length; i++) {
                    areaDetails[i] = parseLong(areaDetailsStr[i]);
                }
                String[] nameDetails = todo.getName().split("_");
                //每一条数据的总长度
                Integer N = areaDetails.length;
                Node nodeFirst;
                //在res表中查看有没有首节点
                Node firstIfHas = getFirst(areaDetails[0], resList);
                //创建首节点
                if (firstIfHas == null) {
                    nodeFirst = new Node(areaDetails[0], nameDetails[0], null, null, new ArrayList<>(), 0);
                    resList.add(nodeFirst);
                } else {
                    nodeFirst = firstIfHas;
                }
                //遍历每一个节点
                for (int i = 1; i < N; i++) {
                    Node parent = get(areaDetails[i - 1], nodeFirst);
                    //如果之前有这个节点了，无需重复添加 跳过
                    if (contain(areaDetails[i], parent.getChildren())) {
                        continue;
                    } else {//否则 创建节点 添加
                        parent.getChildren().add(new Node(areaDetails[i], nameDetails[i], null, null, new ArrayList<>(), 0));
                    }
                }
            }
        }
        return resList;
    }
}
